package com.etrans.web.gps.business.gis.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.conversions.Bson;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.gis.domain.Location;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.util.map.GaoDeConverter;
import com.etrans.web.gps.util.map.GaoDePoint;
import com.etrans.web.gps.util.map.GaoDeUtils;
import com.etrans.web.gps.util.map.GpsToGaoDe;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Service
public class LocationService extends BaseService {

	public List<String> queryVehicleKeys(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return this.selectList("locationMapper.queryVehicleKeys", param);
	}

	public List<Location> getLocation(List<String> keys, int status) {
		if (status != 3) {
			List<Location> result = new ArrayList<>();
			for (int i = 0; i < keys.size(); i = i + 1000) {
				int k = i + 1000;
				if (k > keys.size()) {
					k = keys.size();
				}

				// 每次获取1000台车的报警数据
				List<String> subList = keys.subList(i, k);
				List<Bson> bson = new ArrayList<>();
				DBObject projectFiled1 = new BasicDBObject("_id", "$_id");
				// 如果参数为3停车状态，则查询所有的报警类型
				projectFiled1.put("alarm", "$alarms." + status + ".alarm_type");
				Bson project1 = new BasicDBObject("$project", projectFiled1);
				// 关联实时轨迹表，取方向以及定位时间，经纬度
				DBObject lookupFiled = new BasicDBObject("from", "gps_info_realtime");
				lookupFiled.put("localField", "_id");
				lookupFiled.put("foreignField", "_id");
				lookupFiled.put("as", "gps_filed");
				Bson lookup = new BasicDBObject("$lookup", lookupFiled);

				Bson unwind = new BasicDBObject("$unwind", "$gps_filed");

				DBObject matchField = new BasicDBObject("_id", new BasicDBObject("$in", subList));

				matchField.put("alarm", status);
				Bson match = new BasicDBObject("$match", matchField);
				// 提取所需要的数据，相当于select
				DBObject projectFiled2 = new BasicDBObject("id", "$id");
				projectFiled2.put("alarm", "$alarm");
				projectFiled2.put("lonLat", "$gps_filed.coordinates.coordinates");
				projectFiled2.put("head", "$gps_filed.direction");
				projectFiled2.put("gpstime", "$gps_filed.gps_time");
				projectFiled2.put("gpsSpeed", "$gps_filed.gps_speed");
				Bson project2 = new BasicDBObject("$project", projectFiled2);
				bson.add(project1);
				bson.add(lookup);
				bson.add(unwind);
				bson.add(match);
				bson.add(project2);
				List<Location> location = MGTemplete.aggregate("egov", "alarm_info_realtime", bson, Location.class,	true);
				//经纬度纠偏
				for(Location obj : location){
					if(obj !=null){
						List<String> LngLat = obj.getLonLat();
						double[] GaoDe=GpsToGaoDe.transform(Double.parseDouble(LngLat.get(0)), Double.parseDouble(LngLat.get(1)));
						obj.setLng(GaoDe[0]);
						obj.setLat(GaoDe[1]);
					}
				}
				result.addAll(location);
			}
			return result;
		} else {
			List<Location> result = new ArrayList<>();
			for (int i = 0; i < keys.size(); i = i + 1000) {
				int k = i + 1000;
				if (k > keys.size()) {
					k = keys.size();
				}
				// 每次获取1000台车的轨迹数据
				List<String> subList = keys.subList(i, k);
				List<Bson> bson = new ArrayList<>();
				DBObject projectFiled = new BasicDBObject("_id", "$_id");
				projectFiled.put("lonLat", "$coordinates.coordinates");
				projectFiled.put("head", "$direction");
				projectFiled.put("gpstime", "$gps_time");
				projectFiled.put("gpsSpeed", "$gps_speed");
				Bson project = new BasicDBObject("$project", projectFiled);
				DBObject matchField = new BasicDBObject("_id", new BasicDBObject("$in", subList));
				Bson match = new BasicDBObject("$match", matchField);
				bson.add(project);
				bson.add(match);
				List<Location> location = MGTemplete.aggregate("egov", "gps_info_realtime", bson, Location.class, true);
				for (Location obj : location) {
					if (obj != null) { // 3 停车
						//经纬度纠偏
						List<String> LngLat = obj.getLonLat();
						double[] GaoDe=GpsToGaoDe.transform(Double.parseDouble(LngLat.get(0)), Double.parseDouble(LngLat.get(1)));
						obj.setLng(GaoDe[0]);
						obj.setLat(GaoDe[1]);
						if (obj.getGpsSpeed() < 10) {
							obj.setAlarm(3);
							result.add(obj);
						}

					}
				}
			}
			return result;
		}

	}

	public List<HashMap<String, Object>> queryVeListByRole(HashMap<String, Object> param) {
		return this.selectList("locationMapper.queryVeListByRole", param);
	}

}
